package com.atzxh.guigu.juc.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 小红同学
 * @create 2023-04-27-21:07
 * @description        比较 callable 和 runnable 接口
 */
public class Demo01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // runnable接口创建线程
        new Thread(new MyThread01(),"aa").start();

        // callable接口创建线程
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread02());

        FutureTask<Integer> futureTask1 = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " : come in callable");
            return 1024;
        });
        new Thread(futureTask1,"bb").start();
        new Thread(futureTask,"aa").start();

//        while (!futureTask1.isDone()) {
//            System.out.println("wait............");
//        }
        System.out.println(futureTask.get());
        System.out.println(futureTask1.get());
        System.out.println(Thread.currentThread().getName() + " : over");
    }
}

class MyThread01 implements Runnable{

    @Override
    public void run() {
        System.out.println("实现runnable接口");
    }
}

class MyThread02 implements Callable {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " : come in callable");
        return 200;
    }
}
